package edu.tgc.web.controller.file;

import java.util.HashSet;
import java.util.UUID;

import org.abyss.common.Constants;
import org.abyss.common.exception.ServiceException;
import org.abyss.common.web.controller.easyui.EasyuiAjaxFormResult;
import org.abyss.common.web.controller.easyui.EasyuiResultBuilder;
import org.abyss.data.specifications.Rule;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import edu.tgc.entity.file.FileBidding;
import edu.tgc.entity.file.FilePath;
import edu.tgc.service.file.FileBiddingService;
import edu.tgc.web.form.file.FileBiddingForm;

@Controller
@RequestMapping(value = "file/bidding")
public class FileBiddingController extends AbstractFileResourceController<FileBidding, UUID, FileBiddingForm> {

    @Autowired
    protected FileBiddingService service;


    @RequestMapping(value = "audit", method = RequestMethod.GET)
    public void audit(FileBiddingForm form, BindingResult bindingResult, Model model) {
        FileBidding entity = form.getId() == null ? null : this.baseService.getById(form.getId());
        model.addAttribute(Constants.Variable.OP, "审核");
        model.addAttribute(Constants.Variable.MODEL, entity);
    }

    @ResponseBody
    @RequestMapping(value = "audit", method = RequestMethod.POST)
    public EasyuiAjaxFormResult audit(FileBiddingForm form, BindingResult bindingResult, Model model, RedirectAttributes redirectAttributes) {
        if (bindingResult.hasErrors()) {
            return buildUpdateErrorResult(form, bindingResult.getFieldError().getDefaultMessage());
        } else {
            try {
                this.service.audit(form.getId(), form.getAudit(), form.getDescription());
                // return EasyuiResultBuilder.buildAjaxResult("审核成功");
                return EasyuiResultBuilder.buildAjaxResult();
            } catch (ServiceException e) {
                return buildUpdateErrorResult(form, e.getMessage());
            }
        }
    }

    @Override
    public Specification<FileBidding> buildSearch(FileBiddingForm form) {
        HashSet<Rule> rules = new HashSet<>();
        String searchKey = form.getSearchKey();
        if (StringUtils.hasText(searchKey)) {
            rules.add(Rule.or(Rule.property("name").contain(searchKey)));
        }
        return Rule.and(rules).<FileBidding> toSpecification();
    }

    @Override
    public FilePath getPath() {
        return new FilePath("d:\\file\\bidding");
    }

}
